home *** CD-ROM | disk | FTP | other *** search
- > I'm making a dungeon game its levels are 50x50 stored in an array
- > I need some code to rotate this map 90 degrees in either direction..
-
- For the sake of simplicity, the code below assumes you're useing a
- two-dimensional array to store your map. Obviously, a one-
- dimensional array or particularly a binary array would be more
- effiecient, but you can apply the design to whatever method you're
- actually using...
-
- Okay, the idea is simple enough. To rotate your map counter clock
- wise, you need to transfer the map rows (top to bottom) to the map
- cols (left to right)...
- ABCDE EJOTY
- FGHIJ DINSX
- KLMNO CHMRW
- PQRST BGLQV
- UVWXY AFKPU
-
- And for clockwise rotation, you simply transfer the map rows (top to
- bottom) to the map cols (right to left)...
- ABCDE UPKFA
- FGHIJ VQLGB
- KLMNO WRMHC
- PQRST XSNID
- UVWXY YTOJE
-
- Of course, that is simply the way I am viewing it, you could look
- at the process in a number of ways, but moving on to the code...:)
-
- BTW- I'm on my PC and just did a quick test in C (these routines\
- work fine), so if I make some syntax errors in the AMOS
- version (created on the fly below), you'll know why...
-
- I'm going to use a 5x5 matrix for this example, as my creation code
- fills a 5x5 MAP with the characters in the above examples...
- just change the MAPWIDTH and MAPHEIGHT values to 50.
-
- --------<CODE STARTS HERE>--------
- Dim MAP(50,100) : Rem Double-height BUFFERED map...
- Shared MAP(), MAPBASE, MAPWIDTH, MAPHEIGHT
-
- Procedure ROTATEMAPLEFT
- DESTBASE=MAPHEIGHT-MAPBASE
- DESTCOL=0
- For REP=0 To MAPHEIGHT-1
- SOURCEROW=MAPBASE+REP
- DESTROW=DESTBASE+(MAPHEIGHT-1)
- For SOURCECOL=0 To MAPWIDTH-1
- MAP(DESTCOL,DESTROW)=MAP(SOURCECOL,SOURCEROW)
- Dec DESTROW
- Next SOURCECOL
- Inc DESTCOL
- Next REP
- MAPBASE=DESTBASE
- End Proc
-
- Procedure ROTATEMAPRIGHT
- DESTBASE=MAPHEIGHT-MAPBASE
- DESTCOL=MAPWIDTH-1
- For REP=0 To MAPHEIGHT-1
- SOURCEROW=MAPBASE+REP
- DESTROW=DESTBASE
- For SOURCECOL=0 To MAPWIDTH-1
- MAP(DESTCOL,DESTROW)=MAP(SOURCECOL,SOURCEROW)
- Inc DESTROW
- Next SOURCECOL
- Dec DESTCOL
- Next REP
- MAPBASE=DESTBASE
- End Proc
-
- Procedure MAPVIEW
- For YPOS=0 To MAPHEIGHT-1
- Print " ";
- For XPOS=0 To MAPWIDTH-1
- Print Chr$(MAP(XPOS,MAPBASE+YPOS));
- Next XPOS
- Print
- Next YPOS
- End Proc
-
- ' ----------------------------
- ' MAIN CODE HERE
- '-----------------------------
- MAPBASE=0 : Rem Toggled between 0 and 5 for buffering
- MAPWIDTH=5 : MAPHEIGHT=5
-
- Print "Initializing Map..." : PRINT
- TILE=Asc("A")
- For YPOS=0 To MAPHEIGHT-1
- For XPOS=0 To MAPWIDTH-1
- MAP(XPOS,MAPBASE+YPOS)=TILE
- Add TILE,1,Asc("A") To Asc("Z")
- Next XPOS
- Next YPOS
-
- Print "Map defined as follows:"
- MAPVIEW
-
- Print "Original Map rotated CounterClockWise:"
- ROTATEMAPLEFT
- MAPVIEW
-
- Print "Original Map rotated ClockWise:"
- ROTATEMAPRIGHT : ROTATEMAPRIGHT
- MAPVIEW
-
- Wait Key
- End
- --------<CODE ENDS HERE>--------
-
- Everytime you call these rotation functions, the MAPBASE is
- changed, so you'll need to use MAPBASE+ROW to access your map.
- I used a buffered design as this is faster than having to copy your
- MAP into a temporary workbuffer then rotate "back" to the MAP
- array...
-
- Anyway, hope this helps... :-)
-
-
-
- Garfield Benjamin e-mail:gbenjam@sosbbs.com
- VerticalShooter(PC): 52% complete(...23 days)
- NEW!! (DemoVersion0.52 available on my GAMES-page) NEW!!
- Website( http://www.sosbbs.com/~gbenjam ): 50% Complete
-
-